/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the seckill4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.jalonjia.seckill.core.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jalonjia.seckill.common.core.constant.enums.RoleTypeEnum;
import com.jalonjia.seckill.common.core.util.R;
import com.jalonjia.seckill.common.log.annotation.SysLog;
import com.jalonjia.seckill.common.security.util.SecurityUtils;
import com.jalonjia.seckill.core.entity.SeckillActivity;
import com.jalonjia.seckill.core.entity.SeckillOrder;
import com.jalonjia.seckill.core.entity.SeckillOrderExcelVO;
import com.jalonjia.seckill.core.entity.SeckillTarget;
import com.jalonjia.seckill.core.service.SeckillActivityService;
import com.jalonjia.seckill.core.service.SeckillOrderService;
import com.jalonjia.seckill.core.service.SeckillTargetService;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 秒杀订单表
 *
 * @author 贾晓超
 * @date 2022-05-05 20:56:44
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/seckillorder")
@Tag(name = "秒杀订单表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SeckillOrderController {

	private final SeckillOrderService seckillOrderService;
	private final SeckillTargetService seckillTargetService;

	/**
	 * 分页查询
	 *
	 * @param page         分页对象
	 * @param seckillOrder 秒杀订单表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('core_seckillorder_get')")
	public R getSeckillOrderPage(Page page, SeckillOrder seckillOrder) {
		return seckillOrderService.getSeckillOrderPage(page, seckillOrder);
	}


	/**
	 * 通过id查询秒杀订单表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('core_seckillorder_get')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(seckillOrderService.getById(id));
	}

	/**
	 * 新增秒杀订单表
	 *
	 * @param seckillOrder 秒杀订单表
	 * @return R
	 */
	@Operation(summary = "新增秒杀订单表", description = "新增秒杀订单表")
	@SysLog("新增秒杀订单表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('core_seckillorder_add')")
	public R save(@RequestBody SeckillOrder seckillOrder) {
		return R.ok(seckillOrderService.save(seckillOrder));
	}

	/**
	 * 修改秒杀订单表
	 *
	 * @param seckillOrder 秒杀订单表
	 * @return R
	 */
	@Operation(summary = "修改秒杀订单表", description = "修改秒杀订单表")
	@SysLog("修改秒杀订单表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('core_seckillorder_edit')")
	public R updateById(@RequestBody SeckillOrder seckillOrder) {
		return R.ok(seckillOrderService.updateById(seckillOrder));
	}

	/**
	 * 通过id删除秒杀订单表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除秒杀订单表", description = "通过id删除秒杀订单表")
	@SysLog("通过id删除秒杀订单表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('core_seckillorder_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(seckillOrderService.removeById(id));
	}

	/**
	 * 通过id取消秒杀订单表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id取消秒杀订单表", description = "通过id取消秒杀订单表")
	@SysLog("通过id取消秒杀订单表")
	@GetMapping("/cancel/{id}")
	@PreAuthorize("@pms.hasPermission('core_seckillorder_cancel')")
	public R cancelById(@PathVariable Long id) {
		SeckillOrder order = seckillOrderService.getById(id);
		seckillTargetService.update().eq("id",order.getTargetNo()).setSql("remain_num = remain_num + 1").update();
		boolean update = seckillOrderService.update().eq("id", id).set("status", -1).update();
		return update ? R.ok() : R.failed("取消失败");
	}

	@ResponseExcel
	@GetMapping("/export")
	public List<SeckillOrderExcelVO> export(SeckillOrder seckillOrder) {
		return seckillOrderService.export(seckillOrder);
	}

}
